let GM = require('GM');

let sdk = window.sdk;
cc.Class({
    extends: cc.Component,

    properties: {
        rankUIPrefab: cc.Prefab,   //排行榜UI预制体
        role: cc.Node,             //人物父节点
        openVoice: cc.Node,        //声音开
        closeVoice: cc.Node,       //声音关
        //remindSpt: cc.Node,        //提醒背景图
        tick:cc.Node,              //已经预定

        //remind: cc.Label,          //提醒
        //remindlabel:cc.Label,         //提醒文字
        loading:cc.Node,              //加载
    },

    onLoad(){
        if(sdk){
            sdk.onMain();
        }
    },

    start() {
        this.on_click();
        this.roleIndex = 1;
        if(GM.voice == 1){
            this.openVoice.active = true
        }else{
            this.closeVoice.active = true
        }
    },

    /**
     * 绑定选角按钮事件
     */
    on_click: function () {
        for (let i = 0; i < this.role.childrenCount; i++) {
            let self = this;
            let currentRole = this.role.children[i];
            //给每个角色绑定按钮事件，该角色被点击后放大，其他角色变小
            currentRole.on(cc.Node.EventType.TOUCH_END, function (event) {

                self.roleIndex = event.target.children[0].getComponent('RoleData').myId;

                // 选中的角色放大
                currentRole.runAction(cc.scaleTo(0.2, 1.05, 1.05));

                //其他未选中的角色全部缩放
                for (let j = 0; j < self.role.childrenCount; j++) {
                    let otherRole = self.role.children[j];
                    if (i != j) {
                        otherRole.runAction(cc.scaleTo(0.2, 1, 1));
                    }
                }
            }, this);
        }
    },

    /**
     * 排行榜按钮  ！--判断排行榜UI是否存在，是就显示，不是就生成一个新的排行榜UI
     */
    openRankUI: function () {
        let node;
        if (cc.find("Canvas/Rank") != null) {
            node = cc.find("Canvas/Rank");
            node.active = true;
        } else {
            node = cc.instantiate(this.rankUIPrefab);
            this.node.parent.addChild(node);
        }
        let RankControl = node.getComponent('RankControl');
        RankControl.friendRank();
    },

    /**
     * 提醒开关
     */
    // remindManager: function () {
    //     this.remindSpt.active = false;
    //     this.tick.active = true;
    //     GM.remind = 0;
    // },

    /**
     * 声音开关
     */
    voiceManager: function () {
        this.openVoice.active = !this.openVoice.active;
        this.closeVoice.active = !this.closeVoice.active;
        if(this.openVoice.active == true){
            GM.voice = 1;
        }else if(this.closeVoice.active == true){
            GM.voice = 0;
        }
    },

    /**
     * 开始游戏
     */
    startButton: function () {
        this.loading.active = true;
        GM.roleIndex = this.roleIndex;
        cc.director.preloadScene('game-scene', function(){
            // cc.loader.onProgress = null;
            //加载完毕
            cc.director.loadScene('game-scene');
            if(sdk){
                //关闭子域Main
                sdk.offMain();
            }
        });
    },
    //邀请
    invite(){
        if(sdk){
            sdk.UserInvite({
                success:() => {
                    
                },
                fail:() => {

                }
            })
        }
    },
    //盛唐连接
    sheng(){
        if(sdk){
            sdk.sheng();
        }
           ///sdk
    },
});
